﻿using BLL;
using Common;
using IBLL;
using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using user_defined_Panl;
using static System.Net.Mime.MediaTypeNames;

namespace WindowsFormsApp1.学生
{
    public partial class  Form2 : Form
    {
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        List<List<int>> ints = new List<List<int>>();
        List<int> inte = new List<int>();
        public static DateTime time;
        string e = "";
        public Form2(string id) 
        {
            InitializeComponent();
        
            #region
            e = id;
            int index = 1;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append($"select *from view_SC where Exam_id='{e}';");
            stringBuilder.Append($"select *from view_MC where Exam_id='{e}';");
            stringBuilder.Append($"select *from view_TOF where Exam_id='{e}';");
            stringBuilder.Append($"select *from view_CP where Exam_id=' {e} ';");
            SqlDataReader sqlData = frQuestion.Query(stringBuilder.ToString());
            List<int> ints1 = new List<int>();
            if (sqlData != null)
            {
                while (sqlData.Read())
                {
                    flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_2(new MultipleChoice()
                    {
                        MCID = sqlData["pitchID"].ToString(),
                        Topic = sqlData["Topic"].ToString(),
                        Answer = sqlData["Answer"].ToString(),
                        A = sqlData["A"].ToString(),
                        B = sqlData["B"].ToString(),
                        C = sqlData["C"].ToString(),
                        D = sqlData["D"].ToString(),
                        Score = sqlData["Score"].ToString()
                    }, index.ToString(),false,false));
                    index++;
                }
                inte.Add(flowLayoutPanel1.Controls.Count);
                if (sqlData.NextResult())
                {
                    while (sqlData.Read())
                    {
                        flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_2(new MultipleChoice()
                        {
                            MCID  = sqlData["pitchID"].ToString(),
                            Topic = sqlData["Topic"].ToString(),
                            Answer = sqlData["Answer"].ToString(),
                            A= sqlData["A"].ToString(),
                            B= sqlData["B"].ToString(),
                            C= sqlData["C"].ToString(),
                            D= sqlData["D"].ToString(),
                            Score = sqlData["Score"].ToString()
                        },index.ToString(),false));
                        index++;
                    }
                }
                inte.Add(flowLayoutPanel1.Controls.Count);
                if (sqlData.NextResult())
                {
                    while (sqlData.Read())
                    {
                        flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_3(new TrueOrFalseQuestions()
                        {
                            SPID = sqlData["pitchID"].ToString(),
                            Topic = sqlData["Topic"].ToString(),
                            Answer = sqlData["Answer"].ToString(),
                            Score = sqlData["Score"].ToString()
                        }, index.ToString(),false));
                        index++;
                    }
                }
                inte.Add(flowLayoutPanel1.Controls.Count);
                if (sqlData.NextResult())
                {
                    while (sqlData.Read())
                    {
                        flowLayoutPanel1.Controls.Add(new FlowLayoutPanel_4(new Completion()
                        {
                            CPID= sqlData["pitchID"].ToString(),
                            Topic= sqlData["Topic"].ToString(),
                            Answer = sqlData["Answer"].ToString(),
                            Score=sqlData["Score"].ToString(),
                            ScoringPoint= sqlData["ScoringPoint"].ToString()
                        }, index.ToString(),false));
                        index++;
                    }
                }
                sqlData.Close();
                inte.Add(flowLayoutPanel1.Controls.Count);
            }
            #endregion
        }
        //提交
        private void button1_Click(object sender, EventArgs x)
        {
            StringBuilder builder = new StringBuilder();
            float[]Score= { 0,0,0,0};
          
            for (int i = 0; i < inte[0]; i++)
            {
                builder.Append($"insert into SingleChoice_UserCommit(pitchID, Answer) values('{flowLayoutPanel1.Controls[i].Controls[0].Tag}'");
                bool y = true;
                foreach (var item in flowLayoutPanel1.Controls[i].Controls[1].Controls)
                {
                    if (item is RadioButton)
                    {
                        if ((item as RadioButton).Checked == true)
                        {
                           builder.Append( $" ,'{(item as RadioButton).Text}'); ");
                            string[]s= flowLayoutPanel1.Controls[i].Tag.ToString().Split('|');
                            y = false;
                            if (s[0]== (item as RadioButton).Text)
                            {
                                Score[0] += Convert.ToInt32(s[1]);
                              
                            }
                            break;
                        }
                    }
                }
                if (y)
                {
                    builder.Append(",'');");
                }
            }
            for (int i = inte[0]; i < inte[1]; i++)
            {

                string str = "";
                string[] s = flowLayoutPanel1.Controls[i].Tag.ToString().Split('|');
                float a = 0;
                foreach (var item in flowLayoutPanel1.Controls[i].Controls[1].Controls)
                {
                    if (item is CheckBox)
                    {
                        if ((item as CheckBox).Checked == true)
                        {
                            str += (item as CheckBox).Text;
                            if (a>=0&&s[0].IndexOf((item as CheckBox).Text) !=-1)
                            {
                                a += Convert.ToInt32(s[1]) / s[0].Length;
                            }
                            else
                            {
                                a = -1;
                            }
                           
                        }
                    }

                }
                if (a!=-1)
                {
                    Score[1] += a;
                }
                builder.Append($"insert into MultipleChoice_UserCommit(pitchID, Answer) values('{flowLayoutPanel1.Controls[i].Controls[0].Tag}', '{str}') ;");
            }
            for (int i = inte[1]; i < inte[2]; i++)
            {
                bool y = true;
                builder.Append($"insert into TrueOrFalseQuestions_UserCommit(pitchID, Answer) values('{flowLayoutPanel1.Controls[i].Controls[0].Tag}'");
                foreach (var item in flowLayoutPanel1.Controls[i].Controls[1].Controls)
                {
                    if (item is RadioButton)
                    {
                        if ((item as RadioButton).Checked == true)
                        {
                            builder.Append($", '{(item as RadioButton).Text}'); ");
                            string[] s = flowLayoutPanel1.Controls[i].Tag.ToString().Split('|');
                            if (s[0] == (item as RadioButton).Text)
                            {
                                Score[2] += Convert.ToInt32( s[1]);
                            }
                            y= false;
                            break;
                        }
                    }

                }
                if (y)
                {
                    builder.Append(",'');");
                }
            }
            for (int i = inte[2]; i < inte[3]; i++)
            {
                string[] s = flowLayoutPanel1.Controls[i].Tag.ToString().Split('|');
                string k = s[2];
                string[] s1 = k.Split(";");
                List<string[]> s2 = new List<string[]>();
                for (int y = 0; y < s1.Length; y++)
                {
                    s2.Add(s1[y].Split(":"));
                }
                if (flowLayoutPanel1.Controls[i].Controls[2].Controls[0].Text == s[0])
                {
                    Score[3] += float.Parse(s[1].ToString());
                }
                else
                {
                    for (int m = 0; m < s2.Count; m++)
                    {
                        if (flowLayoutPanel1.Controls[i].Controls[2].Controls[0].Text.IndexOf(s2[m][0]) != -1)
                        {
                            Score[3] += float.Parse(s2[m][1].ToString());
                        }
                    }
                }
                builder.Append($"insert into Completion_UserCommit(pitchID, Answer) values('{flowLayoutPanel1.Controls[i].Controls[0].Tag}', '{flowLayoutPanel1.Controls[i].Controls[2].Controls[0].Text}') ;");
            }
            try
            {
                builder.Append($"insert into Examinee_Score values({Score[0]},{Score[1]},{Score[2]},{Score[3]},'{login_Com.account.Account}','{e}');");
                frQuestion.SubMit(builder.ToString());
                MessageBox.Show("试卷提交成功", "温馨提示");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("试卷提交失败 错误信息:" + ex.Message, "温馨提示");
            }

        }
        //时间
        private void uiButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan timeSpan = time - DAL.SQLHepler.GetDateTime();
            uiLabel1.Text = timeSpan.Hours + ":" + timeSpan.Minutes + ":" + timeSpan.Seconds;
            if (timeSpan.Hours < 0)
            {
                timer1.Stop();
                button1_Click(null, null);
                return;
            }
        }
    }
}
